//
//  DLMachineVideoTC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/28.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLMachineVideoTC.h"
#import "DLMachineModel.h"

@interface DLMachineVideoTC()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, copy) NSString *titleString;


//ZFplayer
@property (nonatomic, strong) UIImageView *coverImageView;
@property (nonatomic, strong) UIButton *playBtn;
@property (nonatomic, weak) id<ZFTableViewCellDelegate> delegate;
@property (nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation DLMachineVideoTC

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self layoutsubControls];
        self.backgroundColor = kDLMachineTableAndCellBgColor;
//        self.backgroundColor = kColorRGB(240, 240, 240);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void) layoutsubControls {
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(kMachineVideoViewLeftMargin, 10, kScreenWidth - kMachineVideoViewLeftMargin *2, kAutoWidth(35));
    titleLabel.font = kSystemFont(14);
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
//    UIImageView *coverImageView = [UIImageView new];
    self.coverImageView.frame = CGRectMake(kMachineVideoViewLeftMargin, 10, kScreenWidth - kMachineVideoViewLeftMargin *2, kMachineVideoViewDefaultHeight - 20);
//    coverImageView.userInteractionEnabled = YES;
//    coverImageView.tag = 100;
//    coverImageView.contentMode = UIViewContentModeScaleAspectFill;
//    coverImageView.clipsToBounds = YES;
    [self.contentView addSubview:self.coverImageView];
//    self.coverImageView = coverImageView;
    
    UIButton *playBtn = [UIButton buttonWithType:0];
    playBtn.frame = CGRectMake((self.coverImageView.width - 44) / 2, (self.coverImageView.height - 44) / 2, 44, 44);
    [playBtn setImage:[UIImage imageNamed:@"coverview_play"] forState:UIControlStateNormal];
    [playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.coverImageView addSubview:playBtn];
    self.playBtn = playBtn;
    
    [self.contentView bringSubviewToFront:titleLabel];
}

- (void) setVideoModel:(DLMachineModel *)videoModel {
    _videoModel = videoModel;
    
    NSDictionary *dataDic = videoModel.resultDic;
    NSString *titleString = [NSString stringWithFormat:@"%@", dataDic[@"title"]];
    self.titleString = titleString;
//    NSString *duration = [NSString stringWithFormat:@"%@", dataDic[@"duration"]];
    NSString *imageBase64String = [NSString stringWithFormat:@"%@", dataDic[@"head_img"]];
    NSArray *arr = [imageBase64String componentsSeparatedByString:@","];
    // 将base64字符串转为NSData
    NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:[arr lastObject] options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
    // 将NSData转为UIImage
    UIImage *decodeImage = [UIImage imageWithData:decodeData];
    
    //title
    self.titleLabel.text = titleString;
    
    
//    NSString *urlString = @"http://imgsrc.baidu.com/forum/eWH%3D240%2C176/sign=183252ee8bd6277ffb784f351a0c2f1c/5d6034a85edf8db15420ba310523dd54564e745d.jpg";
    //    [self.coverImageView setImageWithURLString:urlString placeholder:decodeImage];
    
    [self.coverImageView setImageWithURLString:nil placeholder:decodeImage];
    
//    NSLog(@"视频的cell：%@", decodeImage);
    //视频链接
//    [NSURL URLWithString:@"http://video.static.xialiuda.cn/video-1309e0c8664d4c7797f5bf91c6c7b06d.mp4"]
    
}

- (void)setDelegate:(id<ZFTableViewCellDelegate>)delegate withIndexPath:(NSIndexPath *)indexPath {
    self.delegate = delegate;
    self.indexPath = indexPath;
}

- (void)playBtnClick:(UIButton *)sender {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.titleLabel.alpha = 0;
    } completion:^(BOOL finished) {
        [self.titleLabel removeFromSuperview];
    }];
    
    if ([self.delegate respondsToSelector:@selector(zf_playTheVideoAtIndexPath:)]) {
        [self.delegate zf_playTheVideoAtIndexPath:self.indexPath];
    }
}

- (void)setNormalMode {
    
    self.titleLabel.textColor = [UIColor whiteColor];
//    self.contentView.backgroundColor = [UIColor whiteColor];
}

- (UIImageView *)coverImageView {
    if (!_coverImageView) {
        _coverImageView = [[UIImageView alloc] init];
        _coverImageView.userInteractionEnabled = YES;
        _coverImageView.tag = 100;
        _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
        _coverImageView.clipsToBounds = YES;
    }
    return _coverImageView;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

@end
